﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS  Overseas CountryManagementHelper
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-2-9
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;
using System.Data.Objects;

namespace ASBCC.Business.CIS
{
    /// <summary>
    /// CIS 海外 国家信息模块 数据库访问类
    /// </summary>
    public static class CountryManagementHelper
    {
        /// <summary>
        /// 通过countryID查询areas
        /// </summary>
        public static IEnumerable GetAreaWithCountryRelation(int countryID)
        {
            using (CCEntities dc = new CCEntities())
            {
                var country = dc.Countries.FirstOrDefault(p => p.CountryID == countryID);
                var areas = dc.Areas.Where(p => p.Disabled == false).ToArray();
                country.Areas.Load();
                var source = from p in areas
                             select new
                             {
                                 p.AreaID,
                                 p.Name,
                                 p.Description,
                                 ContainsArea = country.Areas.Contains(p) ? "checked" : "",
                             };
                return source.ToArray();
            }
        }


        /// <summary>
        /// 为Country添加area
        /// </summary>
        public static void OnCountryInserting(int countryID,string name, string description, string areaList)
        {
            using (var dc = new CCEntities())
            {
                if (!String.IsNullOrEmpty(areaList))
                {
                    Country country;
                    if (countryID == 0)
                    {
                        country = new Country();
                        country.Name = name;
                        country.Description = description;
                        dc.AddToCountries(country);
                        dc.SaveChanges();
                        Role role = new Role();
                        role.Name = country.CountryID.ToString();
                        role.Description = "+country+";
                        role.BelongsTo = "CIO";
                        role.Countries.Add(country);
                        dc.AddToRoles(role);
                    }
                    else
                    {
                        country = dc.Countries.First(p => p.CountryID == countryID);
                    }
                    
                    foreach (var area in areaList.Split(','))
                    {
                        int areaID = int.Parse(area);

                        var CountryInArea = dc.Areas.First(p => p.AreaID == areaID);

                        country.Areas.Add(CountryInArea);

                    }
                    dc.SaveChanges();

                }
            }
        }

        /// <summary>
        /// 为country修改area
        /// </summary>
        public static void OnCountryUpdating(int countryID,string name, string description, string areaList)
        {
            using (var dc = new CCEntities())
            {
                Country country = dc.Countries.First(p => p.CountryID == countryID);
                country.Areas.Load();
                country.Areas.Clear();
                dc.SaveChanges();
                OnCountryInserting(countryID,name, description, areaList);
            }

        }
        /// <summary>
        /// 将一个Country的激活状态设为未激活
        /// </summary>
        public static void SetCountryInfoDisabled(int countryID)
        {
            using (var dc = new CCEntities())
            {
                dc.Countries.FirstOrDefault(p => p.CountryID == countryID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Countries.FirstOrDefault(p => p.CountryID == entityID).Name;
                return name;
            }
        }
    }
}
